//
//  PFRootTBVC.m
//  PFDevelopKit
//
//  Created by 原鹏飞 on 2018/10/30.
//  Copyright © 2018 ypf. All rights reserved.
//

#import "PFRootTBVC.h"

#import "PFRootTBCell.h"
#import "PFRootTBModel.h"

#import "PFTestVCOne.h"
#import "PFTestVCTwo.h"

@interface PFRootTBVC ()

@property (nonatomic, strong) NSMutableArray<UIViewController *> *vcArr;

@end

@implementation PFRootTBVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self prepareData];
    [self setupUI];
}


- (void)prepareData {
    // 控制器数组作为TB的数据源
    _vcArr = [NSMutableArray array];
    
    PFTestVCOne *vcOne = PFTestVCOne.alloc.init;
    [_vcArr addObject:vcOne];
    
    PFTestVCTwo *vcTwo = PFTestVCTwo.alloc.init;
    [_vcArr addObject:vcTwo];
}


- (void)setupUI {
    [self.tableView registerClass:PFRootTBCell.class forCellReuseIdentifier:NSStringFromClass(PFRootTBCell.class)];
    
    
    
    
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.vcArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PFRootTBCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(PFRootTBCell.class) forIndexPath:indexPath];
    cell.nameStr = NSStringFromClass(self.vcArr[indexPath.row].class);
    //    cell.nameStr = [NSString stringWithFormat:@"%zd组, %zd行", indexPath.section, indexPath.row];
    
    // 用RAC设置cell按钮的跳转
    ASWeak(self);
    [[[cell.nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:cell.rac_prepareForReuseSignal] subscribeNext:^(__kindof UIControl * _Nullable x) {
        UIViewController *vc = [weakself queryFatherVC];
        NSLog(@"%@", vc);
        [vc presentViewController:weakself.vcArr[indexPath.row] animated:YES completion:^{

        }];
//        [vc pushViewController:weakself.vcArr[indexPath.row] animated:YES];
        NSLog(@"%@", NSStringFromClass(weakself.vcArr[indexPath.row].class));
    }];
    
    return cell;
}


- (UIViewController *)queryFatherVC {
    for (UIView* next = [self.view superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return NO;
    }
    return YES;
}


// 设置编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}


- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删删删";
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"想要删除这个cell：第%zd组 第%zd行", indexPath.section, indexPath.row);
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"选中cell：第%zd组 第%zd行", indexPath.section, indexPath.row);
}


//- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
//
//}



/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
